package algorithm.MaxSubstrNoRepetition;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/*
在一个字符串中找到没有重复字符子串中最长的长度
 */
public class MaxSubstrNoRepetition {
    public static int lengthOfLongestSubstring(String s) {
        int l = s.length()-1;
        int r = s.length()-1;
        Map<Character, Integer> map = new HashMap<>();
        int max = 0;

        while (l >= 0) {
            Integer rep = map.get(s.charAt(l));
            if (rep != null && rep - 1 < r) {
                r = rep - 1;
            }
            max = Math.max(max, r-l+1);
            map.put(s.charAt(l), l);
            l--;
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }

}
